'use strict';
const BaseController = require('./base');
const dayjs = require('dayjs');
const { success } = require('../../extend/api.js');
const { filterBody } = require('../../extend/helper.js');
const UserService = require('../../service/api/user.js');

class UserController extends BaseController {

  constructor(props) {
    super(props);
  }

  // 搜索
  static async search(req, res, next) {
    try {
      const data = await UserService.search(req.query);
      data.list.forEach(ele => {
        ele.loginTime = dayjs(ele.loginTime).format('YYYY-MM-DD HH:mm:ss');
        ele.registerTime = dayjs(ele.registerTime).format('YYYY-MM-DD HH:mm:ss');
      });
      res.json({ ...success, data: data });
    } catch (error) {
      next(error);
    }
  }
  static async detail(req, res, next) {
    try {
      const id = req.query.id;
      const data = await UserService.detail(id);
      res.json({ ...success, data: data });
    } catch (error) {
      next(error);
    }
  }
static async recharge(req, res, next){
  try {
    const body = req.body;
    const data = await UserService.recharge(body);
    console.error("-------------------------------"+data+"---------------------------------");
    res.json({ ...success, data: data });
  } catch (error) {
    next(error);
  }
}
  // 删除
static async delete(req, res, next) {
  try {
    const id = req.query.id;
    const data = await UserService.delete(id);
    res.json({ ...success, data: data });
  } catch (error) {
    next(error);
  }
}
  static async update(req, res, next) {
    try {
      const body = req.body;
      body.registerTime = dayjs(body.registerTime).format('YYYY-MM-DD HH:mm:ss');
      body.loginTime = dayjs(body.loginTime).format('YYYY-MM-DD HH:mm:ss');
      const data = await UserService.update(body);
      console.error("-------------------------------"+data+"---------------------------------");
      res.json({ ...success, data: data });
    } catch (error) {
      next(error);
    }
  }

}

module.exports = UserController;